﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

namespace TaskManager.Web.Controls
{
    /// <summary>
    /// Tabs container
    /// </summary>
    public class TabContainer
    {
        private readonly List<Tab> _tabs;
        private int _nextTabId = DefaultTabId;

        /// <summary>
        /// Identifier of the default tab
        /// </summary>
        public const int DefaultTabId = 0;

        /// <summary>
        /// Parameterless constructor
        /// </summary>
        public TabContainer()
        {
            var defaultTab = new Tab(string.Empty, string.Empty, DefaultTabId, string.Empty, false);

            _tabs = new List<Tab> {defaultTab};
            ActiveTab = 0;

            _nextTabId++;
        }

        /// <summary>
        /// Tabs collection
        /// </summary>
        public IEnumerable<Tab> Tabs { get { return _tabs; } }

        /// <summary>
        /// Active tab
        /// </summary>
        public int ActiveTab { get; set; }

        /// <summary>
        /// Total tabs count
        /// </summary>
        public int Count { get { return _tabs.Count; } }

        /// <summary>
        /// Indexing operator
        /// </summary>
        /// <param name="tabId">Tab identifier</param>
        /// <returns>Tab</returns>
        public Tab this[int tabId]
        {
            get
            {
                int index = GetTabIndexById(tabId);
                return _tabs[index];
            }
        }

        /// <summary>
        /// Adds new tab
        /// </summary>
        /// <param name="tabName">Tab title</param>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="urlHelper">Url builder</param>
        /// <param name="routeValues">Route values collection</param>
        /// <returns></returns>
        public Tab AddTab(string tabName, string actionName, string controllerName, UrlHelper urlHelper, RouteValueDictionary routeValues)
        {
            string contentId = urlHelper.Action(actionName, controllerName, routeValues);

            // Check if page with given URL is already opened
            Tab tab = _tabs.Where(t => t.ContentId == contentId).SingleOrDefault();

            if (tab != null)
                return tab;

            int newTabId = _nextTabId++;

            routeValues["tab"] = newTabId;
            
            string url = urlHelper.Action(actionName, controllerName, routeValues);

            var newTab = new Tab(tabName, url, newTabId, contentId, closable: true);
            _tabs.Add(newTab);

            ActiveTab = newTabId;

            return newTab;
        }

        /// <summary>
        /// Removes all tabs except the default tab (index=0)
        /// </summary>
        public void RemoveAll()
        {
            if (_tabs.Count < 2)
                return;

            _tabs.RemoveRange(1, _tabs.Count-1);
        }

        /// <summary>
        /// Closes the specified tab and set active the preceding tab
        /// </summary>
        /// <param name="tabId">Tab identifier</param>
        public void Close(int tabId)
        {
            int index = GetTabIndexById(tabId);

            if(index != 0)
            {
                ActiveTab = index - 1;
                _tabs.RemoveAt(index);
            }
            else
            {
                ActiveTab = 0;
            }
        }

        private int GetTabIndexById(int tabId)
        {
            for (int i = 0; i < _tabs.Count; i++)
            {
                if (_tabs[i].TabId == tabId)
                {
                    return i;
                }
            }

            return -1;
        }
    }
}